/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Dao.exceptions.IllegalOrphanException;
import Dao.exceptions.NonexistentEntityException;
import Dao.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Logico.Deportista;
import java.util.ArrayList;
import java.util.List;
import Logico.Evento;
import Logico.Competencia;
import Logico.Disciplina;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author usuario
 */
public class DisciplinaJpaController implements Serializable {

    public DisciplinaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    protected EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Disciplina disciplina) throws PreexistingEntityException, Exception {
        if (disciplina.getDeportistaList() == null) {
            disciplina.setDeportistaList(new ArrayList<Deportista>());
        }
        if (disciplina.getEventoList() == null) {
            disciplina.setEventoList(new ArrayList<Evento>());
        }
        if (disciplina.getCompetenciaList() == null) {
            disciplina.setCompetenciaList(new ArrayList<Competencia>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Deportista> attachedDeportistaList = new ArrayList<Deportista>();
            for (Deportista deportistaListDeportistaToAttach : disciplina.getDeportistaList()) {
                deportistaListDeportistaToAttach = em.getReference(deportistaListDeportistaToAttach.getClass(), deportistaListDeportistaToAttach.getCedula());
                attachedDeportistaList.add(deportistaListDeportistaToAttach);
            }
            disciplina.setDeportistaList(attachedDeportistaList);
            List<Evento> attachedEventoList = new ArrayList<Evento>();
            for (Evento eventoListEventoToAttach : disciplina.getEventoList()) {
                eventoListEventoToAttach = em.getReference(eventoListEventoToAttach.getClass(), eventoListEventoToAttach.getIdevento());
                attachedEventoList.add(eventoListEventoToAttach);
            }
            disciplina.setEventoList(attachedEventoList);
            List<Competencia> attachedCompetenciaList = new ArrayList<Competencia>();
            for (Competencia competenciaListCompetenciaToAttach : disciplina.getCompetenciaList()) {
                competenciaListCompetenciaToAttach = em.getReference(competenciaListCompetenciaToAttach.getClass(), competenciaListCompetenciaToAttach.getIdcompetencia());
                attachedCompetenciaList.add(competenciaListCompetenciaToAttach);
            }
            disciplina.setCompetenciaList(attachedCompetenciaList);
            em.persist(disciplina);
            for (Deportista deportistaListDeportista : disciplina.getDeportistaList()) {
                deportistaListDeportista.getDisciplinaList().add(disciplina);
                deportistaListDeportista = em.merge(deportistaListDeportista);
            }
            for (Evento eventoListEvento : disciplina.getEventoList()) {
                eventoListEvento.getDisciplinaList().add(disciplina);
                eventoListEvento = em.merge(eventoListEvento);
            }
            for (Competencia competenciaListCompetencia : disciplina.getCompetenciaList()) {
                Disciplina oldDisciplinaIddisciplinaOfCompetenciaListCompetencia = competenciaListCompetencia.getDisciplinaIddisciplina();
                competenciaListCompetencia.setDisciplinaIddisciplina(disciplina);
                competenciaListCompetencia = em.merge(competenciaListCompetencia);
                if (oldDisciplinaIddisciplinaOfCompetenciaListCompetencia != null) {
                    oldDisciplinaIddisciplinaOfCompetenciaListCompetencia.getCompetenciaList().remove(competenciaListCompetencia);
                    oldDisciplinaIddisciplinaOfCompetenciaListCompetencia = em.merge(oldDisciplinaIddisciplinaOfCompetenciaListCompetencia);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findDisciplina(disciplina.getIddisciplina()) != null) {
                throw new PreexistingEntityException("Disciplina " + disciplina + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Disciplina disciplina) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Disciplina persistentDisciplina = em.find(Disciplina.class, disciplina.getIddisciplina());
            List<Deportista> deportistaListOld = persistentDisciplina.getDeportistaList();
            List<Deportista> deportistaListNew = disciplina.getDeportistaList();
            List<Evento> eventoListOld = persistentDisciplina.getEventoList();
            List<Evento> eventoListNew = disciplina.getEventoList();
            List<Competencia> competenciaListOld = persistentDisciplina.getCompetenciaList();
            List<Competencia> competenciaListNew = disciplina.getCompetenciaList();
            List<String> illegalOrphanMessages = null;
            for (Competencia competenciaListOldCompetencia : competenciaListOld) {
                if (!competenciaListNew.contains(competenciaListOldCompetencia)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Competencia " + competenciaListOldCompetencia + " since its disciplinaIddisciplina field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Deportista> attachedDeportistaListNew = new ArrayList<Deportista>();
            for (Deportista deportistaListNewDeportistaToAttach : deportistaListNew) {
                deportistaListNewDeportistaToAttach = em.getReference(deportistaListNewDeportistaToAttach.getClass(), deportistaListNewDeportistaToAttach.getCedula());
                attachedDeportistaListNew.add(deportistaListNewDeportistaToAttach);
            }
            deportistaListNew = attachedDeportistaListNew;
            disciplina.setDeportistaList(deportistaListNew);
            List<Evento> attachedEventoListNew = new ArrayList<Evento>();
            for (Evento eventoListNewEventoToAttach : eventoListNew) {
                eventoListNewEventoToAttach = em.getReference(eventoListNewEventoToAttach.getClass(), eventoListNewEventoToAttach.getIdevento());
                attachedEventoListNew.add(eventoListNewEventoToAttach);
            }
            eventoListNew = attachedEventoListNew;
            disciplina.setEventoList(eventoListNew);
            List<Competencia> attachedCompetenciaListNew = new ArrayList<Competencia>();
            for (Competencia competenciaListNewCompetenciaToAttach : competenciaListNew) {
                competenciaListNewCompetenciaToAttach = em.getReference(competenciaListNewCompetenciaToAttach.getClass(), competenciaListNewCompetenciaToAttach.getIdcompetencia());
                attachedCompetenciaListNew.add(competenciaListNewCompetenciaToAttach);
            }
            competenciaListNew = attachedCompetenciaListNew;
            disciplina.setCompetenciaList(competenciaListNew);
            disciplina = em.merge(disciplina);
            for (Deportista deportistaListOldDeportista : deportistaListOld) {
                if (!deportistaListNew.contains(deportistaListOldDeportista)) {
                    deportistaListOldDeportista.getDisciplinaList().remove(disciplina);
                    deportistaListOldDeportista = em.merge(deportistaListOldDeportista);
                }
            }
            for (Deportista deportistaListNewDeportista : deportistaListNew) {
                if (!deportistaListOld.contains(deportistaListNewDeportista)) {
                    deportistaListNewDeportista.getDisciplinaList().add(disciplina);
                    deportistaListNewDeportista = em.merge(deportistaListNewDeportista);
                }
            }
            for (Evento eventoListOldEvento : eventoListOld) {
                if (!eventoListNew.contains(eventoListOldEvento)) {
                    eventoListOldEvento.getDisciplinaList().remove(disciplina);
                    eventoListOldEvento = em.merge(eventoListOldEvento);
                }
            }
            for (Evento eventoListNewEvento : eventoListNew) {
                if (!eventoListOld.contains(eventoListNewEvento)) {
                    eventoListNewEvento.getDisciplinaList().add(disciplina);
                    eventoListNewEvento = em.merge(eventoListNewEvento);
                }
            }
            for (Competencia competenciaListNewCompetencia : competenciaListNew) {
                if (!competenciaListOld.contains(competenciaListNewCompetencia)) {
                    Disciplina oldDisciplinaIddisciplinaOfCompetenciaListNewCompetencia = competenciaListNewCompetencia.getDisciplinaIddisciplina();
                    competenciaListNewCompetencia.setDisciplinaIddisciplina(disciplina);
                    competenciaListNewCompetencia = em.merge(competenciaListNewCompetencia);
                    if (oldDisciplinaIddisciplinaOfCompetenciaListNewCompetencia != null && !oldDisciplinaIddisciplinaOfCompetenciaListNewCompetencia.equals(disciplina)) {
                        oldDisciplinaIddisciplinaOfCompetenciaListNewCompetencia.getCompetenciaList().remove(competenciaListNewCompetencia);
                        oldDisciplinaIddisciplinaOfCompetenciaListNewCompetencia = em.merge(oldDisciplinaIddisciplinaOfCompetenciaListNewCompetencia);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = disciplina.getIddisciplina();
                if (findDisciplina(id) == null) {
                    throw new NonexistentEntityException("The disciplina with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Disciplina disciplina;
            try {
                disciplina = em.getReference(Disciplina.class, id);
                disciplina.getIddisciplina();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The disciplina with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Competencia> competenciaListOrphanCheck = disciplina.getCompetenciaList();
            for (Competencia competenciaListOrphanCheckCompetencia : competenciaListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Disciplina (" + disciplina + ") cannot be destroyed since the Competencia " + competenciaListOrphanCheckCompetencia + " in its competenciaList field has a non-nullable disciplinaIddisciplina field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Deportista> deportistaList = disciplina.getDeportistaList();
            for (Deportista deportistaListDeportista : deportistaList) {
                deportistaListDeportista.getDisciplinaList().remove(disciplina);
                deportistaListDeportista = em.merge(deportistaListDeportista);
            }
            List<Evento> eventoList = disciplina.getEventoList();
            for (Evento eventoListEvento : eventoList) {
                eventoListEvento.getDisciplinaList().remove(disciplina);
                eventoListEvento = em.merge(eventoListEvento);
            }
            em.remove(disciplina);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Disciplina> findDisciplinaEntities() {
        return findDisciplinaEntities(true, -1, -1);
    }

    public List<Disciplina> findDisciplinaEntities(int maxResults, int firstResult) {
        return findDisciplinaEntities(false, maxResults, firstResult);
    }

    private List<Disciplina> findDisciplinaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Disciplina.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Disciplina findDisciplina(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Disciplina.class, id);
        } finally {
            em.close();
        }
    }

    public int getDisciplinaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Disciplina> rt = cq.from(Disciplina.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
